package com.kingint.web.domain;


import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.mbg.sys.entity.UmsPermission;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员详情封装
 * https://gitee.com/zscat-platform/mall on 2018/8/3.
 */
public class MemberDetails implements UserDetails {
    private UmsMember umsMember;
    private List<UmsPermission> permissionList;

    public MemberDetails(UmsMember umsMember) {
        this.umsMember = umsMember;
    }

    public MemberDetails(UmsMember umsMember, List<UmsPermission> permissionList) {
        this.umsMember = umsMember;
        this.permissionList = permissionList;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //返回当前用户的权限
//        return Arrays.asList(new SimpleGrantedAuthority("TEST"));
        if (permissionList.size() != 0) {
            return permissionList.stream()
                    .filter(permission -> permission.getValue() != null)
                    .map(permission -> new SimpleGrantedAuthority(permission.getValue()))
                    .collect(Collectors.toList());
        } else {
            return Arrays.asList(new SimpleGrantedAuthority("TEST"));
        }
    }

    @Override
    public String getPassword() {
        return umsMember.getPassWord();
    }

    @Override
    public String getUsername() {
        return umsMember.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return umsMember.getStatus() == 1;
    }

    public UmsMember getUmsMember() {
        return umsMember;
    }
}
